	package com.acando.mate;

import java.util.List;

import com.acando.mate.models.Worker;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class WorkersAdapter extends ArrayAdapter<Worker> {

	public WorkersAdapter(Context context, List<Worker> objects) {
		super(context, R.layout.news_item, objects);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.worker_item, parent, false);
		
		TextView workers_surname = (TextView) rowView
				.findViewById(R.id.workers_surname);
		TextView workers_name = (TextView) rowView
				.findViewById(R.id.workers_name);
		
		TextView mobile = (TextView) rowView
				.findViewById(R.id.workers_mobil_view);
		mobile.setOnClickListener(phoneClickListener);
		TextView buro = (TextView) rowView
				.findViewById(R.id.workers_buro_view);
		buro.setOnClickListener(phoneClickListener);
		TextView mail = (TextView) rowView
				.findViewById(R.id.workers_mail_view);
		TextView placePermanent = (TextView) rowView
				.findViewById(R.id.workers_place_permanent_view);
		TextView placeTemporary = (TextView) rowView
				.findViewById(R.id.workers_place_temporary_view);
		
		
		workers_surname.setText(getItem(position).getSurname());
		workers_name.setText(getItem(position).getName());
		mobile.setText(getItem(position).getMobile());
		buro.setText(getItem(position).getBuro());
		mail.setText(getItem(position).getEmail());
		placePermanent.setText(getItem(position).getPermaPlace().getName());
		placeTemporary.setText(getItem(position).getTempPlace().getName());
		return rowView;
	}

	private OnClickListener phoneClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			try {

				String phoneNumber = ((TextView) v).getText().toString();
				Intent callIntent = new Intent(Intent.ACTION_CALL);
				callIntent.setData(Uri.parse("tel:" + phoneNumber));
				getContext().startActivity(callIntent);
			} catch (Exception e) {
				Log.e("WorkersAdapter",
						"do not use phoneClickListener if you have no Phone in text-field");
			}
		}
	};

}
